package com.jvyou.mybatis.demo.plugin;

import com.jvyou.mybatis.executor.parameter.ParameterHandler;
import com.jvyou.mybatis.plugin.Intercepts;
import com.jvyou.mybatis.plugin.Invocation;
import com.jvyou.mybatis.plugin.PluginInterceptor;
import com.jvyou.mybatis.plugin.Signature;

import java.sql.PreparedStatement;
import java.util.List;
import java.util.Properties;

/**
 * @author 橘柚
 * @version 1.0-SNAPSHOT
 * @Date 2024/8/3 12:59
 * @Description
 */
@Intercepts({
        @Signature(type = ParameterHandler.class, method = "setParameters", args = {PreparedStatement.class, List.class, Object.class})
})
public class SqlLogParamPlugin implements PluginInterceptor {
    @Override
    public Object intercept(Invocation invocation) {
        // 先执行方法
        Object result = invocation.proceed();
        // 获取第一个参数，第一个参数为 PreparedStatement 对象
        PreparedStatement ps = (PreparedStatement) invocation.getArgs()[0];
        String sql = ps.toString().replace("com.mysql.cj.jdbc.ClientPreparedStatement:", "").trim();
        System.out.println("SqlLogParamPlugin:" + sql);
        return result;
    }

    @Override
    public void setProperties(Properties properties) {

    }
}
